import { def } from "./utils";

const arrayPrototye = Array.prototype;
// console.log(arrayPrototye)

export const arrayMethods = Object.create(arrayPrototye);
// console.log(arrayMethods)

const methodsNeedChange = [
  'push',
  'pop',
  'shift',
  'unshift',
  'splice',
  'sort',
  'reverse'
]

methodsNeedChange.forEach(methodName => {
  const original = arrayPrototye[methodName];

  def(arrayMethods, methodName, function (...args) {
    console.log(arguments)
    console.log('大道店')
    const result = original.apply(this, args);

    const ob = this.__ob__
    let inserted
    switch (methodName) {
      case 'push':
      case 'unshift':
        inserted = args;
        break;
      case 'splice':
        inserted = args.slice(2);
        break;
    }

    if (inserted) {
      ob.observeArray(inserted)
    }

    return result
  })
})
